var ajax_server = 'http://sunjl.hmcq.cn:8080/';
var ajax_server = 'http://s94.top/';
//rem
function set_rem(){
	console.log(displayMetrics);
	var scale = displayMetrics.wdp / displayMetrics.wpx;
	set_rem.META.content = 'maximum-scale='+scale+',minimum-scale='+scale+',user-scalable=no,width=device-width,initial-scale=1';
	window['dprem'] = 100 * ((displayMetrics.wdp>640?640:displayMetrics.wdp)/640);
	window['rem'] = window['dprem']/scale;
	document.documentElement.style.fontSize = window['rem']+'px';
}
!function(){
	var meta = document.querySelectorAll('meta[name="viewport"]');
	meta = meta.length ? meta[meta.length-1] : document.createElement('meta');
	if(meta.name != 'viewport'){
    	meta.name = 'viewport';
    	document.querySelector('head').appendChild(meta);
    }
	meta.content = "width=device-width, initial-scale=1, maximum-scale=1";
	var wdp = document.documentElement.clientWidth;
	var hdp = document.documentElement.clientHeight;
	var scale = 1/(window.devicePixelRatio || 1);
	meta.content = 'maximum-scale='+scale+',minimum-scale='+scale+',user-scalable=no,width=device-width,initial-scale=1';
	var wpx = document.documentElement.clientWidth;
	var hpx = document.documentElement.clientHeight;
	window['displayMetrics'] = {
		wdp: wdp,wpx: wpx,
		hdp: hdp,hpx: hpx,
	}
	set_rem.META = meta;
	set_rem();
}()
$.app&&$.app(function(){
	window['displayMetrics']={
		wdp: api.winWidth,wpx: api.screenWidth,
		hdp: api.winHeight,hpx: api.screenHeight,
	}
	set_rem();
	if(api.safeArea.top) $('header.public').css('padding-top',(api.safeArea.top*api.screenWidth/api.winWidth)+'px');
    if(api.safeArea.bottom) $('footer.public').css('padding-bottom',(api.safeArea.bottom*api.screenWidth/api.winWidth)+'px');
})
/*提示*/
function tips(msg,time){
	if(!msg){ return; }
	var setup={msg: msg,global: true}
	time && (setup['duration']=time);
	$.app(function(){api.toast(setup)})
}
//统一的ajax请求处理
function ajax_request(url,data,callback,op){
	url = url.indexOf('http')===0 ? url : ajax_server+url;
	var reqlist = ajax_request.reqlist = ajax_request.reqlist || {};//请求队列，记录每个请求的地址
	var thislist = reqlist[url] = reqlist[url] || {count_req:0,count_success:0,count_error:0};
	data = data || '';
	op = op || {};
	if(op.cache){
		var cache_key = url + (url.indexOf('?') === -1?'?':'&') + $.param(data);
		var cache_data = $.app.cache(cache_key);
		cache_data && typeof(op.cache)=="function" && op.cache(cache_data);
	}
	if (thislist.runing) return;
	thislist.runing=true;
	$.app(function(){
		$.ajax({
			type:"post",
			url:url,
			data:data,
			success:function(res){
				thislist.count_success++;
				if(!res.code && res.data && res.data.need_login){
					$.app.cache('user/user_info','');
					return $.app.win('user/login');
				}
				if(op.cache) $.app.cache(cache_key, res);
				if(typeof(callback)=="function" && callback(res)!==false && res.msg) tips(res.msg);
			},
			complete:function(ret){
				thislist.count_req++;
				thislist.runing=undefined;
			},
			error:function(err){
				thislist.count_error++;
				$.log(err);
				tips('网络繁忙，请稍后再试!');
			}
		});
	})
}
/**
 * 全局特殊a标签事件绑定。标签自定义参数说明：
 * href：页面名称，如book/account_log，back和close为特别关键字，表示关闭
 * target：是否已window打开，_blank为以window打开，其他均为frame
 * reload：存在且不为空表示打开后刷新，否则为不刷新
 */
function a_onclick(){
	$('a[app]').each(function(){
		if(this.a_onclick) return;
		this.a_onclick=true;
		var $this = $(this);
		$this.on('click',function(){
			var href = $this.attr('href');
			if (href.indexOf('javascript:')===0) return false;
			window.event.stopPropagation();
			window.event.preventDefault();
			var reload = !!$this.attr('reload');
			var is_win = $this.attr('target')=='_blank';
			if(href == 'back' || href == 'close'){
				is_win ? $.app.win('', false) : $.app.frame('', false);
			}else{
				is_win ? $.app.win(href, {reload: reload}) : $.app.frame(href, {reload: reload});
			}
		})
	})
}
$(function(){a_onclick()})
/**
 * 窗口重新显示时刷新或者执行回调函数
 * @param {Object} callback
 */
function show_reload(callback){
	var hidden = false;
	api.addEventListener({name: 'viewappear'}, function(ret, err){
		hidden && typeof(callback) == 'function' && callback();
	});
	api.addEventListener({name: 'viewdisappear'}, function(ret, err){hidden = true;});
}

function img(src, callback){
	api.imageCache({url: src,thumbnail:false}, function(ret, err) {
		typeof(callback)=="function" && callback(ret.url);
	});
}
/**
 * 加载中蒙版
 * @param {Object} open
 */
function loading_mask(open){
	if(open){
		app.frame('public/loading');
	}else{
		app.frame('public/loading', false);
	}
}
/**
 * 选取图片，并裁剪，上传
 * @param {Object} setup 配置参数【主要有：lockAspectRatio; quality; forceWidth】
 * @param {Object} callback 回调函数，接收上传后的http地址
 */
function form_img(setup, callback){
	setup = setup || {};
	if(typeof(callback)=="function"){
		var ac_name = 'form_img_cb' + (new Date()).getTime();
		setup.callback = ac_name;
		window[ac_name] = callback;
	}
	$.app(function(){
		setup.w_name = api.winName;
		setup.f_name = api.frameName;
		$.app.win('public/form_img', {
			pageParam: setup
		});
	})
}

function select_file(setup, callback){
	setup = setup || {};
	if(typeof(callback)=="function"){
		var ac_name = 'select_file_cb' + (new Date()).getTime();
		setup.callback = ac_name;
		window[ac_name] = callback;
	}
	$.app(function(){
		setup.w_name = api.winName;
		setup.f_name = api.frameName;
		$.app.win('public/file_list', {
			pageParam: setup
		});
	})
}